﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using LY.Entity;
using LY.Entity.PageSearch;
using LY.Entity.SearchEntity;
using LY.Entity.Jsons;
using LY.Interface;
using LY.Common.Utils;

namespace QJ.CMS.Areas.Admin.Controllers
{
    public class DictController : Controller
    {
        //
        // GET: /Admin/Dict/
        private IDictService _iservice;
        
        public DictController(IDictService iService)
        {
            _iservice = iService;
        }

        public ActionResult DictDirList()
        {
            return View();
        }

        //用户列表
        [HttpPost]
        public JsonResult DictDirListDetail(DictDirSearch search, FormCollection form)
        {
            PageView view = new PageView(form);
            string colkey = form["colkey"];
            string colsinfo = form["colsinfo"];
            PagedList<S_DictDir> mList = _iservice.QueryDictDirList(search, view);
            JsonQTable fdata = JsonQTable.ConvertFromPagedList<S_DictDir>(mList, colkey, colsinfo.Split(','));
            return Json(fdata);
        }

        //编辑用户
        public ActionResult DictDirEdit(int? id)
        {
            S_DictDir dictdir = null;
            if (!id.HasValue)
            {
                dictdir = new S_DictDir();
            }
            else
            {
                dictdir = _iservice.GetDictDir(id);
            }
            return View(dictdir);
        }
        //编辑用户 保存
        [HttpPost]
        public JsonResult DictDirSave(int? id, S_DictDir dictdir, FormCollection collection)
        {
            JsonSMsg rMsg = new JsonSMsg();
            try
            {
                string errmsg = "";
                //if (!Validate(dictdir, out errmsg))
                //{
                //    rMsg.Status = -1;
                //    rMsg.Message = errmsg;
                //    return Json(rMsg);
                //}
                //string ss = collection["IsStop"];
                int ret = _iservice.SaveDictDir(dictdir);
                if (ret > 0)
                {
                    rMsg.Status = 1;
                    rMsg.Data = ret;
                }
                else if (ret == -2)
                {
                    rMsg.Status = -1;
                    rMsg.Message = "系统中已存在相同的用户名！";
                }
                else
                {
                    rMsg.Status = -1;
                    rMsg.Message = "意外错误，请稍后重试！";
                }
            }
            catch (Exception ex)
            {
                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            return Json(rMsg);
        }

        ///// <summary>
        ///// 参数验证
        ///// </summary>
        ///// <param name="user"></param>
        ///// <param name="errmsg"></param>
        ///// <returns></returns>
        //private static bool Validate(S_User user, out string errmsg)
        //{
        //    errmsg = "";
        //    if (user == null)
        //    {
        //        errmsg = "参数错误！";
        //        return false;
        //    }

        //    if (string.IsNullOrEmpty(user.LoginName))
        //    {
        //        errmsg = "用户账号不能为空！";
        //        return false;
        //    }

        //    if (!string.IsNullOrEmpty(user.Email) && !ValidateUtil.IsEmail(user.Email))
        //    {
        //        errmsg = "邮箱格式有误！";
        //        return false;
        //    }
        //    return true;
        //}

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DictDirDelete(int id, FormCollection collection)
        {
            JsonSMsg rMsg = new JsonSMsg();
            try
            {
                if (id <= 0)
                {
                    rMsg.Status = -1;
                    rMsg.Message = "参数错误！";
                    return Json(rMsg);
                }

                int ret = _iservice.DeleteDictDir(id);
                if (ret > 0)
                {
                    rMsg.Status = 1;
                    rMsg.Data = ret;
                }
                else
                {
                    rMsg.Status = -1;
                    rMsg.Message = "意外错误，请稍后重试！";
                }
            }
            catch (Exception ex)
            {
                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            return Json(rMsg);
        }

        #region 字典项

        public ActionResult DictItemList()
        {
            return View();
        }

        //用户列表
        [HttpPost]
        public JsonResult DictItemListDetail(DictItemSearch search, FormCollection form)
        {
            PageView view = new PageView(form);
            string colkey = form["colkey"];
            string colsinfo = form["colsinfo"];
            PagedList<S_DictItem> mList = _iservice.QueryDictItemList(search, view);
            JsonQTable fdata = JsonQTable.ConvertFromPagedList<S_DictItem>(mList, colkey, colsinfo.Split(','));
            return Json(fdata);
        }

        //编辑用户
        public ActionResult DictItemEdit(int? id)
        {
            S_DictItem dictitem = null;
            if (!id.HasValue)
            {
                dictitem = new S_DictItem();
            }
            else
            {
                dictitem = _iservice.GetDictItem(id);
            }
            return View(dictitem);
        }
        //编辑用户 保存
        [HttpPost]
        public JsonResult DictItemSave(S_DictItem dictitem, FormCollection collection)
        {
            JsonSMsg rMsg = new JsonSMsg();
            try
            {
                string errmsg = "";

                dictitem.DirID = Convert.ToInt32(collection["DictDirId"].ToString());
                int ret = _iservice.SaveDictItem(dictitem);
                if (ret > 0)
                {
                    rMsg.Status = 1;
                    rMsg.Data = ret;
                }
                else if (ret == -2)
                {
                    rMsg.Status = -1;
                    rMsg.Message = "系统中已存在相同的字典项！";
                }
                else
                {
                    rMsg.Status = -1;
                    rMsg.Message = "意外错误，请稍后重试！";
                }
            }
            catch (Exception ex)
            {
                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            return Json(rMsg);
        }

        ///// <summary>
        ///// 参数验证
        ///// </summary>
        ///// <param name="user"></param>
        ///// <param name="errmsg"></param>
        ///// <returns></returns>
        //private static bool Validate(S_User user, out string errmsg)
        //{
        //    errmsg = "";
        //    if (user == null)
        //    {
        //        errmsg = "参数错误！";
        //        return false;
        //    }

        //    if (string.IsNullOrEmpty(user.LoginName))
        //    {
        //        errmsg = "用户账号不能为空！";
        //        return false;
        //    }

        //    if (!string.IsNullOrEmpty(user.Email) && !ValidateUtil.IsEmail(user.Email))
        //    {
        //        errmsg = "邮箱格式有误！";
        //        return false;
        //    }
        //    return true;
        //}

        /// <summary>
        /// 删除用户
        /// </summary>
        /// <param name="id"></param>
        /// <param name="collection"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult DictItemDelete(int id, FormCollection collection)
        {
            JsonSMsg rMsg = new JsonSMsg();
            try
            {
                if (id <= 0)
                {
                    rMsg.Status = -1;
                    rMsg.Message = "参数错误！";
                    return Json(rMsg);
                }

                int ret = _iservice.DeleteDictItem(id);
                if (ret > 0)
                {
                    rMsg.Status = 1;
                    rMsg.Data = ret;
                }
                else
                {
                    rMsg.Status = -1;
                    rMsg.Message = "意外错误，请稍后重试！";
                }
            }
            catch (Exception ex)
            {
                rMsg.Status = -1;
                rMsg.Message = ex.Message;
            }
            return Json(rMsg);
        }
        #endregion
    }
}
